package cate.game.train.exp;

import cate.common.util.GameResult;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import cate.game.train.exp.msg.ExpOffResp;
import cate.game.train.exp.msg.ExpOnResp;
import org.apache.commons.lang3.StringUtils;

public class TrainExpHandler {
    private Hero findHero(Role role, String heroUid) {
        if (StringUtils.isBlank(heroUid)) {
            return null;
        }
        return role.getBag().hero.getItem(heroUid);
    }

    public GameResult<Void> onExp(Role role, String heroUid, int expTid, byte pos) {
        GameResult<Void> r = new GameResult<>();
        Hero hero = findHero(role, heroUid);
        if (hero == null) {
            return r.fail("英雄不存在");
        }
        GameResult<Byte> onRes = hero.onExp(role.toPlayBuildContext(), expTid, pos);
        if (!onRes.ok()) {
            return r.fail(onRes);
        }
        role.sendNow(new ExpOnResp(heroUid, expTid, pos));
        return r;
    }

    public GameResult<Void> offExp(Role role, String heroUid, byte pos) {
        GameResult<Void> r = new GameResult<>();
        Hero hero = findHero(role, heroUid);
        if (hero == null) {
            return r.fail("英雄不存在");
        }
        GameResult<Byte> onRes = hero.offExp(role.toPlayBuildContext(), pos);
        if (!onRes.ok()) {
            return r.fail(onRes);
        }
        role.sendNow(new ExpOffResp(heroUid, pos));
        return r;
    }
}
